package com.itheima.mm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mm.dto.PasswordDto;
import com.itheima.mm.dto.UserinfoDto;
import com.itheima.mm.mapper.APermissionPermissionGroupMapper;
import com.itheima.mm.mapper.LoginMapper;
import com.itheima.mm.mapper.PePermissionGroupMapper;
import com.itheima.mm.mapper.PePermissionMapper;
import com.itheima.mm.pojo.APermissionPermissionGroup;
import com.itheima.mm.pojo.BsUser;
import com.itheima.mm.dto.Login;
import com.itheima.mm.pojo.PePermission;
import com.itheima.mm.pojo.PePermissionGroup;
import com.itheima.mm.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, BsUser> implements LoginService {

    @Autowired
    private PePermissionMapper pePermissionMapper;

    @Autowired
    private APermissionPermissionGroupMapper aPermissionPermissionGroupMapper;

    @Autowired
    private PePermissionGroupMapper pePermissionGroupMapper;

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private  HttpServletRequest req;

    @Override
    public BsUser login(Login login) {

        LambdaQueryWrapper<BsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BsUser::getEmail,login.getUsername());
        wrapper.eq(BsUser::getPassword,login.getPassword());
        BsUser one = loginMapper.selectOne(wrapper);
        if (one==null){
            return null;
        }
        return one;
    }
    @Override
    public String register(Login login) {
        BsUser bsUser = new BsUser();
        bsUser.setEmail(login.getUsername());//登录邮箱
//        String md5DigestAsHex = DigestUtils.md5DigestAsHex(login.getPassword().getBytes());//有可能MD5加密
        bsUser.setPassword(login.getPassword());//密码
        bsUser.setCreateTime(LocalDateTime.now());//创建时间
        bsUser.setLastUpdateTime(LocalDateTime.now());//最新更新时间
        bsUser.setStatus(0);//账号状态默认启用0

        loginMapper.insert(bsUser);

        return "注册成功";
    }

    @Override
    public String revampPassword(PasswordDto passwordDto) {
        HttpSession session = req.getSession();
        Integer id = (Integer) session.getAttribute("id");
        BsUser bsUser = loginMapper.selectById(id);
        if (!bsUser.getPassword().equals(passwordDto.getOldPassword())){
            return "旧密码错误";
        }
        if (passwordDto.getNewPassword().equals(passwordDto.getOldPassword())){
            return "新密码不能与旧密码相同";
        }
        bsUser.setPassword(passwordDto.getNewPassword());
        loginMapper.updateById(bsUser);
        return "修改成功";
    }

    @Override
    public UserinfoDto profile() {
       HttpSession session = req.getSession();

       Integer id = (Integer) session.getAttribute("id");
       System.err.println(id);
//
        UserinfoDto userinfoDto = new UserinfoDto();
       BsUser bsUser = loginMapper.selectById(id);

       System.err.println(bsUser);
       userinfoDto.setName(bsUser.getUserName());//设置用户名
       userinfoDto.setAvatar(bsUser.getAvatar());//设置头像
       userinfoDto.setRoles(bsUser.getRole());//设置权限
       PePermissionGroup pePermissionGroup = pePermissionGroupMapper.selectById(bsUser.getPermissionGroupId());
       userinfoDto.setGroup(pePermissionGroup.getName());//设置部门

       LambdaQueryWrapper<APermissionPermissionGroup> wrapper = new LambdaQueryWrapper<>();
       wrapper.eq(APermissionPermissionGroup::getPgid,pePermissionGroup.getId());
       List<APermissionPermissionGroup> aPermissionPermissionGroups = aPermissionPermissionGroupMapper.selectList(wrapper);
       List<Long> list =new ArrayList();
       for (APermissionPermissionGroup aPermissionPermissionGroup : aPermissionPermissionGroups) {
           list.add(aPermissionPermissionGroup.getPid());
       }
       LambdaQueryWrapper<PePermission> wapper = new LambdaQueryWrapper<>();
       wapper.in(PePermission::getId,list);
       List<PePermission> pePermissions = pePermissionMapper.selectList(wapper);
       List<String> Menus = new ArrayList<>();
       for (PePermission pePermission : pePermissions) {
           Menus.add(pePermission.getName());
       }
       userinfoDto.setMenus(Menus);//设置菜单
       userinfoDto.setSignature(null);//权限点
       userinfoDto.setPoints(null);//签名
        return userinfoDto;
    }
}
